package Atop100.hash;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Class49 {

        public List<List<String>> groupAnagrams(String[] strs) {
            List<List<String>> res = new ArrayList<>();
            if (strs == null || strs.length == 0) {
                return res;
            }
            Map<String, List<String>> map = new HashMap<>();
            for (String str : strs) {
                int[] counts = new int[26];
                for (int i = 0; i < str.length(); i++) {
                    counts[str.charAt(i) - 'a']++;
                }
                StringBuilder sb = new StringBuilder();
                for (int count : counts) {
                    sb.append(' ').append(count);
                }
                String key = sb.toString();
                if (!map.containsKey(key)) {
                    map.put(key, new ArrayList<>());
                }
                map.get(key).add(str);
            }
            return new ArrayList<>(map.values());
        }

    public List<List<String>> groupAnagrams1(String[] strs) {
        List<List<String>> res = new ArrayList<>();
        //TODO:2023/9/15  记得边界条件
        if (strs == null || strs.length == 0) {
            return res;
        }
      //TODO:2023/9/15 第一步统计每个string的特性  如何统计 根据26个英文字母
        HashMap<String, List<String>> map = new HashMap<>();
        for (String s: strs) {
            int[] count=new int[26];
            for (int i = 0; i < s.length(); i++) {
                count[s.charAt(i)-'a']++;
            }
            StringBuffer sb = new StringBuffer();
            for (int a: count) {
                sb.append(' ').append(a);
//                sb.append(a);
            }
            String s1 = sb.toString();
            //TODO:2023/9/15 通过上面的方式将含有相同字母的单词的格式统一起来了  有一个共同的标识
            if (!map.containsKey(s1)){
                map.put(s1,new ArrayList<String>());
            }
                map.get(s1).add(s);
        }
        //TODO:2023/9/15 返回数值搞错啦
        return  new ArrayList(map.values());
    }


    public static void main(String[] args) {

        Class49 class49 = new Class49();
        String [] s={"bdddddddddd","bbbbbbbbbbc"};
        List<List<String>> lists = class49.groupAnagrams(s);
        System.out.println(lists.toString());
        List<List<String>> list = class49.groupAnagrams1(s);
        System.out.println(list.toString());
    }

    }
